<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | License  https://gitee.com/fudaoji/KyPHP/blob/master/LICENSE
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * Script Name: Category.php
 * Create: 2020/11/16 18:01
 * Description: 类目管理
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mini\controller;

use ky\MiniPlatform\Request\WxaGetCategory;
use ky\MiniPlatform\Request\WxaGetShowWxaItem;
use ky\MiniPlatform\Request\WxaUpdateShowWxaItem;
use ky\MiniPlatform\Request\WxOpenDeleteCategory;
use ky\MiniPlatform\Request\WxOpenGetCategory;

class Category extends Base
{
    /**
     * @var array
     */
    private $auditStatus;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->auditStatus = [
            1 => '审核中',
            2 => '审核不通过',
            3 => '审核通过'
        ];
    }

    /**
     * 首页
     * @return mixed
     * Author: fudaoji<fdj@kuryun.cn>
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @throws \Exception
     */
    public function index(){
        $request = new WxOpenGetCategory();
        $response = $this->getClient()->setCheckRequest(false)->execute($request, $this->getAccessToken());
        if($response['errcode'] !== 0){
            $this->error($response['errmsg']);
        }

        $list = $response['categories'];
        $this->assign['data_list'] = $list;
        $this->assign['audit_status'] = $this->auditStatus;
        return $this->show();
    }

    /**
     * 删除
     * @deprecated 通过api创建的小程序才可以调用
     * @throws \Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function delPost(){
        if(request()->isPost()){
            $post_data = input('post.');
            if(empty($post_data['first']) || empty($post_data['second'])){
                $this->error('参数错误');
            }
            $request = new WxOpenDeleteCategory();
            $request->setFirst($post_data['first']);
            $request->setSecond($post_data['second']);
            $response = $this->getClient()->execute($request, $this->getAccessToken());
            if($response['errcode'] !== 0){
                $this->error($response['errmsg']);
            }
            $this->success('操作成功');
        }
    }

    public function test(){
        $request = new WxaGetShowWxaItem();
        $response = $this->getClient()->setCheckRequest(false)->execute($request, $this->getAccessToken());
        if($response['errcode'] !== 0){
            $this->error($response['errmsg']);
        }
        /**
         * {
        ["errcode"] => int(0)
        ["errmsg"] => string(6) "成功"
        ["can_open"] => int(1)
        ["is_open"] => int(1)
        ["appid"] => string(18) "wx3c24a824fb6f3e1d"
        ["nickname"] => string(9) "爱儿本"
        ["headimg"] => string(81) "http://wx.qlogo.cn/mmhead/Q3auHgzwzM6OPm7DdEAa0ADnon6AWHsAoiaL6BKDqmr7lrwsZl4sowQ"
        }
         */
        $request = new WxaUpdateShowWxaItem();
        $request->setAppId($this->miniInfo['appid']);
        $request->setWxaSubscribeBizFlag(0);
        $response = $this->getClient()->execute($request, $this->getAccessToken());

        dump($response);exit;
    }
}